#ifndef _STATE_HOLDER_H_
#define _STATE_HOLDER_H_

#include "luaplus/LuaPlus.h"
#include "num/named_set.h"

namespace script 
{
class StateHolder
{
public:
							StateHolder();
							~StateHolder();
	void					Load(const std::string& Name);
	void					Close();
	void					BuildIndex(const std::string& Table, const std::string& Name);
	int						GetIndex(const std::string& Name);
	LuaPlus::LuaObject		GetTable(const std::string& Name);		//! get a generic table
	LuaPlus::LuaObject		GetNode(const std::string& Name);		//! Get a cached node
	LuaPlus::LuaState*		GetState() { return m_pState; }


	std::string				GetString(const std::string& Table, const std::string& Name, const std::string& Default = "");
	float					GetFloat(const std::string& Table, const std::string& Name, float Default = 0);
	int						GetInt(const std::string& Table, const std::string& Name, int Default = 0);
	bool					GetBool(const std::string& Table, const std::string& Name, bool Default = true);

	bool					IsValid();
protected:
	LuaPlus::LuaState*		m_pState; 
	struct	NodeData
	{
		int					Index;
		std::string			Name;
	};
	num::NamedSet<NodeData*>	m_Index;
	std::string					m_IndexName, m_KeyName;
};


template <class DATA_TYPE>
class	StateDataAbstract : public script::StateHolder
{
public:
	DATA_TYPE*				GetData(const std::string& Name) { return m_Data.GetElement(Name); };
	DATA_TYPE*				GetDataByIdx(int Idx) { return m_Data.GetElementByIndex(Idx); };
	int						GetCount() { return m_Data.GetElementC(); };
	virtual void			Reload()  = 0;
protected:
	num::NamedSet<DATA_TYPE*>		m_Data;
	StateDataAbstract() { };	// VFT not yet initialized. Can't call reload here :(
	virtual ~StateDataAbstract(){};
};
}



#endif // _STATE_HOLDER_H_